package io.aicn.lowcode.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.aicn.lowcode.entity.Position;
import io.aicn.lowcode.mapper.PositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PositionService extends ServiceImpl<PositionMapper, Position> implements IPositionService {
    
    @Autowired
    private PositionMapper positionMapper;
    
    public List<Position> searchPositions(String title, String code, String category, BigDecimal minSalary, BigDecimal maxSalary, Integer level, String status) {
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Position::getTitle, title);
        }
        if (StringUtils.hasText(code)) {
            queryWrapper.like(Position::getCode, code);
        }
        if (StringUtils.hasText(category)) {
            queryWrapper.like(Position::getDescription, category);
        }
        if (minSalary != null) {
            queryWrapper.ge(Position::getMinSalary, minSalary);
        }
        if (maxSalary != null) {
            queryWrapper.le(Position::getMaxSalary, maxSalary);
        }
        if (level != null) {
            queryWrapper.eq(Position::getLevel, level);
        }
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Position::getStatus, status);
        }
        
        return positionMapper.selectList(queryWrapper);
    }
    
    public Position createPosition(Position position) {
        position.setCreatedAt(LocalDateTime.now());
        position.setUpdatedAt(LocalDateTime.now());
        
        positionMapper.insert(position);
        return position;
    }
    
    public Position updatePosition(Long id, Position position) {
        Position existingPosition = positionMapper.selectById(id);
        if (existingPosition == null) {
            return null;
        }
        
        if (position.getTitle() != null) {
            existingPosition.setTitle(position.getTitle());
        }
        if (position.getCode() != null) {
            existingPosition.setCode(position.getCode());
        }
        if (position.getDescription() != null) {
            existingPosition.setDescription(position.getDescription());
        }
        if (position.getLevel() != null) {
            existingPosition.setLevel(position.getLevel());
        }
        if (position.getMinSalary() != null) {
            existingPosition.setMinSalary(position.getMinSalary());
        }
        if (position.getMaxSalary() != null) {
            existingPosition.setMaxSalary(position.getMaxSalary());
        }
        if (position.getDepartmentId() != null) {
            existingPosition.setDepartmentId(position.getDepartmentId());
        }
        if (position.getStatus() != null) {
            existingPosition.setStatus(position.getStatus());
        }
        
        existingPosition.setUpdatedAt(LocalDateTime.now());
        positionMapper.updateById(existingPosition);
        return existingPosition;
    }
    
    public boolean deletePosition(Long id) {
        return positionMapper.deleteById(id) > 0;
    }
}